package com.ruoyi.file.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDictType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.file.domain.FileDictType;
import com.ruoyi.file.service.IFileDictTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文件字典类型Controller
 * 
 * @author rww
 * @date 2023-02-20
 */
@RestController
@RequestMapping("/file/dict/type")
public class FileDictTypeController extends BaseController
{
    @Autowired
    private IFileDictTypeService fileDictTypeService;

    /**
     * 查询文件字典类型列表
     */
    @PreAuthorize("@ss.hasPermi('fileDictType:fileDictType:list')")
    @GetMapping("/list")
    public TableDataInfo list(FileDictType fileDictType)
    {
        startPage();
        List<FileDictType> list = fileDictTypeService.selectFileDictTypeList(fileDictType);
        return getDataTable(list);
    }

    /**
     * 导出文件字典类型列表
     */
    @PreAuthorize("@ss.hasPermi('fileDictType:fileDictType:export')")
    @Log(title = "文件字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FileDictType fileDictType)
    {
        List<FileDictType> list = fileDictTypeService.selectFileDictTypeList(fileDictType);
        ExcelUtil<FileDictType> util = new ExcelUtil<FileDictType>(FileDictType.class);
        util.exportExcel(response, list, "文件字典类型数据");
    }

    /**
     * 获取文件字典类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('fileDictType:fileDictType:query')")
    @GetMapping(value = "/{dictId}")
    public AjaxResult getInfo(@PathVariable("dictId") Long dictId)
    {
        return success(fileDictTypeService.selectFileDictTypeByDictId(dictId));
    }

    /**
     * 新增文件字典类型
     */
    @PreAuthorize("@ss.hasPermi('fileDictType:fileDictType:add')")
    @Log(title = "文件字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FileDictType dict)
    {
        if (UserConstants.NOT_UNIQUE.equals(fileDictTypeService.checkDictTypeUnique(dict)))
        {
            return error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(getUsername());
        return toAjax(fileDictTypeService.insertFileDictType(dict));

    }

    /**
     * 修改文件字典类型
     */
    @PreAuthorize("@ss.hasPermi('fileDictType:fileDictType:edit')")
    @Log(title = "文件字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FileDictType dict)
    {

        if (UserConstants.NOT_UNIQUE.equals(fileDictTypeService.checkDictTypeUnique(dict)))
        {
            return error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(getUsername());
        return toAjax(fileDictTypeService.updateDictType(dict));

    }

    /**
     * 删除文件字典类型
     */
    @PreAuthorize("@ss.hasPermi('fileDictType:fileDictType:remove')")
    @Log(title = "文件字典类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictIds}")
    public AjaxResult remove(@PathVariable Long[] dictIds)
    {
        return toAjax(fileDictTypeService.deleteFileDictTypeByDictIds(dictIds));
    }


    @PreAuthorize("@ss.hasPermi('fileDictType:fileDictType:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        fileDictTypeService.resetDictCache();
        return success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<FileDictType> dictTypes = fileDictTypeService.selectDictTypeAll();
        return success(dictTypes);
    }
}
